#include <btn.hpp>
#include <utils.hpp>

static int BTN_PIN[12] = { 11, 12, 13, 14, 21, 47, 48, 45, 15, 16, 17, 18 };

#define BTN_PINSET ((1ull << BTN_PIN[0])   \
					| (1ull << BTN_PIN[1]) \
					| (1ull << BTN_PIN[2]) \
					| (1ull << BTN_PIN[3]) \
					| (1ull << BTN_PIN[4]) \
					| (1ull << BTN_PIN[5]) \
					| (1ull << BTN_PIN[6]) \
					| (1ull << BTN_PIN[7]) \
					| (1ull << BTN_PIN[8]) \
					| (1ull << BTN_PIN[9]) \
					| (1ull << BTN_PIN[10]) \
					| (1ull << BTN_PIN[11]))

Btn::Btn()
	: GPIO(E_GPIO_INPUT, BTN_PINSET)
	, m_BtnFilter(0.1f)
	, m_BtnThreshold(0.5f)
{
	for (int i = 0; i < 12; i++)
	{
		m_BtnVal[i]	   = 0.0f;
		m_BtnValPre[i] = 0.0f;
	}
}

Btn::~Btn()
{
}

void Btn::Init()
{
}

void Btn::RunImpl(uint64_t& btn)
{
	btn = 0;
	for (int i = 0; i < 12; i++)
	{
		float val	= (float)GetValue(BTN_PIN[i]);
		m_BtnVal[i] = FilterLowPass(val, m_BtnValPre[i], m_BtnFilter);

		if (m_BtnVal[i] <= m_BtnThreshold)
		{
			btn |= 1ull << i;
		}
	}
	// LOGR("BTN: %04x\n", btn);
}
